
***********************************************************************************************;
*   Code Name: Part2IncludeAssets.sas
  	Authors: Per Siden and Thomas Jansson (Sveriges Riksbank)
	Created: February 6, 2013
	Last edited: April 15, 2015
***********************************************************************************************;

* This is the second step in a series of codes used in this project.
  The original thought is to run the codes in the following order:
	1. Part1InitializeLinda.sas
	2. Part2IncludeAssets.sas
	3. Part3CreateHHVariables.sas
	4. Part4RegionalImmigrantRatio.sas

* In this file we include asset data (f�rm�genhetsdata) for all individuals and
  merge with the previously created databases.
***********************************************************************************************;

DM 'CLEAR LOG';
DM 'CLEAR OUTPUT';
Proc datasets LIB = work KILL;

***********************************************************************************************;
* Define what variables to extract, some of them year-specific;
%let varsallyears = nybidnr ffstegm ffstejm ffstfrm ffstjbm
					ffsthym ffsthyo ffstinm ffstsf ffstum
					ftomtem ftomtfm fkubank fkurfon fopmv
					fovrmv fkuprem ffors faktibmv fotcmv 
					faalmv fkurta fkuskop fskust
					;
%let vars1999 = fborm fkuvp fbfonmv fakfmv fovrtmv fskurst;
%let vars2000to2001 = fborm fkuvpr fbfonmv fakfmv fovrtmv fskurst;
%let vars2002to2004 = fborm fkuvpr fofomv fovrtmv fskurst;
%let vars2005to2006 = fbormny fkuvpr fofomv fovrtmv fskurst;
%let vars2007 =  fbormny fkuvpr fofomv;
***********************************************************************************************;

* This macro includes all interesting asset variables;
%macro m2a(f_or_i);
%do y = 1999 %to 2007;

* Create year-specific macro variables;
%if &y = 1999 %then %do;
	%let vars_to_keep = &varsallyears &vars1999; %end;
%else %if &y < 2002 %then %do;
	%let vars_to_keep = &varsallyears &vars2000to2001; %end;
%else %if &y < 2005 %then %do;
	%let vars_to_keep = &varsallyears &vars2002to2004; %end;
%else %if &y < 2007 %then %do;
	%let vars_to_keep = &varsallyears &vars2005to2006; %end;
%else %do; %let vars_to_keep = &varsallyears &vars2007; %end;

* Giant data step for extracting;
data temp.Lform&f_or_i&y(drop = &vars_to_keep);
	set original.Lform&f_or_i&y(keep = &vars_to_keep);

	id = nybidnr; 										* Individual id;
		label id = 'Individual id';
	house = ffstegm + ffstejm;							* House value;
		label house = 'House value';
	vacationhouse = ffstfrm;							* Vacation house;
		label vacationhouse = 'Vacation house value';
	farms = ffstjbm - ffstejm;							* Farms;
		label farms = 'Farms value';
	multifamilyhouse = ffsthym + ffsthyo; 				* Multi-family house value;
		label multifamilyhouse = 'Multi-family house value';
	industryhouse = ffstinm + ffstsf;					* Industry buildings;
		label industryhouse = 'Industry buildings value';
	foreignhouse = ffstum;								* Foreign estates;
		label foreignhouse = 'Foreign estates value';
	landvalue = ftomtem + ftomtfm;						* Land value;
		label landvalue = 'Land Value';
	
	%if &y < 2005 %then %do;							* Coop apartment;
		coop = fborm;
			label coop = 'Coop apartment value (unreliable)';
	%end;
	%else %do;
		coop = fbormny;
			label coop = 'Coop apartment value (unreliable)';
	%end;

	realassets = house + coop + vacationhouse + farms + multifamilyhouse + 
				  	  industryhouse + foreignhouse + landvalue;
		label realassets = 'Real assets total value';	* Real assets total value;

	bank = fkubank;										* Bank account value;
		label bank = 'Bank accounts value';
	premiumbonds = fkuprem;								* Lottery bonds;
		label premiumbonds = 'Premium bonds';
	othersecurities = fopmv + fovrmv;					* Options and other securities;	
		label othersecurities = 'Options and other securities';
	insurance = ffors;									* Insurance products;
		label insurance = 'Insurances value';
	
	%if &y = 1999 %then %do;							* Bonds;
		bonds = fkurfon + fkuvp;							
		label bonds = 'Bonds value';
	%end;
	%else %do;
		bonds = fkurfon + fkuvpr;							
			label bonds = 'Bonds value';
	%end;
	
	mutualinterest = fkurfon;							* Interest rate mutual funds;
		label mutualinterest = 'Interest rate mutual funds value';
	
	%if &y < 2002 %then %do;							* Mixed and equity mutual funds;
		mutualequity = fakfmv;	
			label mutualequity = 'Mutual equity funds value';	
		mutualmixed = fbfonmv;
			label mutualmixed = 'Mixed mutual funds value';
		mutualother	= mutualequity + mutualmixed;
			label mutualother = 'Mutual fonds total (not interest)';
	%end;
	%else %do;
		mutualequity = .;	
			label mutualequity = 'Mutual equity funds value';	
		mutualmixed = .;
			label mutualmixed = 'Mixed mutual funds value';
		mutualother	= fofomv;
			label mutualother = 'Mutual fonds total (not interest)';
	%end;

	stocks = faktibmv + fotcmv + faalmv; 				* Stock value;
		label stocks = 'Stocks value';
	debtstudies = fskust; 								* Student debt;
		label debtstudies = 'Debt from studies';

	%if &y < 2007 %then %do;							
		debt = sum(of fkurta fkuskop fskurst);			* Debt;
			label debt = 'Debt';
		otherassets = fovrtmv;							* Other non-specified assets;
			label otherassets = 'Other non-specified assets';
	%end;
	%else %do;
		debt = sum(of fkurta fkuskop);					* Debt;
			label debt = 'Debt';
		otherassets = 0;								* Other non-specified assets;
			label otherassets = 'Other non-specified assets';
	%end;
	
	fanobank = bonds + othersecurities + insurance + 
			   premiumbonds	+ mutualother + stocks;		* Financial assets excluding bank accounts;
		label fanobank = 'Financial assets excluding bank account'; 
	risky = stocks + mutualother + othersecurities + insurance;
		label risky = 'Risky financial assets';			* Risky financial assets;
	riskfree = bank + bonds + premiumbonds;				* Risk free financial assets;
		label riskfree = 'Risk free financial assets';
	stocksmutual = stocks + mutualother;				* Stocks and mutual funds;
		label stocksmutual = 'Stocks and mutual funds'; 	
	finw = risky + riskfree;							* Financial assets total;
		label finw = 'Financial assets total';
	totalassets	= sum(of realassets finw otherassets);  * Total asset value;
		label totalassets = 'Total assets value';
	netw = totalassets - debt;							* Net wealth;
		label netw = 'Net wealth';
run;
	
%end;
%mend;
%m2a(i);
%m2a(f);

***********************************************************************************************;
* Merge with the standard LINDA (from code Part1InitializeLinda.sas);

%macro m2b(f_or_i);
%do y = 1999 %to 2007;

proc sort data = temp.Lform&f_or_i&y;
	by id;
run;

proc sort data = temp.linda&f_or_i&y;
	by id;
run;

data temp.lindamerged&f_or_i&y;
	merge temp.linda&f_or_i&y(in = left) temp.Lform&f_or_i&y;
	by id;
	if left;
run;

%end;
%mend;
%m2b(f);
%m2b(i);
***********************************************************************************************;
